package MainProgram;

import Algorithms.BackPropagation;
import Algorithms.Pruning;
import NeuronNetworkLibrary.Network;
import java.util.Arrays;

/**
 * Selective prunning algorithm.
 * NOTE: NOT TESTED!
 * @author Zbyszko
 */
public class SelectivePruneMain {
          
    public final static int EPOCH = 1000;

    public static void main(String[] args) {

        SinusCreator data = new SinusCreator(100);
        //SimpleDataCreator data = new SimpleDataCreator();
        int initialNeuronsNbr = 100;
        
        
        // Create and build the network.
        Network network = new Network(data.getTrainingSet(), data.getDesiredOutput(),
                new int[]{initialNeuronsNbr}, new String[]{"sigmo"}, "linear");

        Pruning prune = new Pruning(network, 0.05);
        int count = prune.pruneSelective();
        System.out.println("neuron deleted counter: " + count);
        //network = prune.getNetwork();
        
        
        
        if(count == initialNeuronsNbr) {
            //System.out.println("system exit: ");
            count = prune.getMinNumber();
            System.out.println("min-neurons: " + count);
            //System.exit(0);
        } else {
            count = initialNeuronsNbr - count;
        }
        

        
//        for (int i = 0; i < network.getOutputLayer().size(); i++) {
//            double weights[] = network.getOutputLayer().get(i).getInputWeights();
//            double newWeights[] = new double[count];
//            int k = 0;
//            for (int j = count; j < weights.length; j++) {
//                newWeights[k] = weights[j];
//                k++;
//            }
//            network.getOutputLayer().get(i).setInputWeights(newWeights); 
//        }
        Network.INIT_WEIGHTS_FLAG = true;
         network = new Network(data.getTrainingSet(), data.getDesiredOutput(),
                new int[]{count}, new String[]{"sigmo"}, "linear");
        
        //*
        int i = 0;
        int order[]; 
        do {
           order = Main.randomizeOrder(network.getNumberOfPatterns());
            for (int j = 0; j < network.getNumberOfPatterns(); j++) {
                network.calculateNetwork(order[j]);
                BackPropagation.calculateBackPropagation(network);
            }
            i++;
        } while (i <= EPOCH);

        String units = Arrays.toString(network.getNeuronsInHiddenLayers());
        Plot plot = new Plot(
                "Neural Network", //window title 
                "Selective pruning with "+ units +" hidden units.", //plot title
                data.getTrainingSet(), //training set 
                data.getDesiredOutput(), //desired output
                Main.printFinalResults(network) //obtained result
                );
        plot.setVisible(true);
        //*/
        ;
    }
}